package com.pmsystem.service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.FreeCarMapper;
import com.pmsystem.dao.UserMapper;

import com.pmsystem.pojo.User;
import com.pmsystem.pojo.UserExample;
import com.pmsystem.pojo.UserExample.Criteria;
import com.pmsystem.util.MD5;

@Service
public class UserService {

	@Autowired
	private UserMapper userMapper;
	@Autowired
	private FreeCarMapper freeCarMapper;

	
	/**
	 * 
	 * <p>Title: login</p >  
	 * <p>Description: 业主登陆</p >  
	 * @author 泽强
	 * @param username
	 * @param password
	 * @return
	 */
	public User login(String username, String password) {
		String encodePwd = MD5.MD5Eencryption(password, username);

		if(encodePwd==null)
			return null;
		
		return userMapper.login(username,encodePwd);

	}


	/**
	 * 
	 * <p>Title: update</p >  
	 * <p>Description: 业主更改信息</p >  
	 * @author 泽强
	 * @param user
	 * @return
	 */
	public boolean update(User user) {
		int flag=userMapper.updateByPrimaryKeySelective(user);	
		return flag==1?true:false;
	}


	/**  
	 * <p>Title: regist</p >  
	 * <p>Description:业主注册</p >
	 * @author 魏家洛
	 * @param user
	 * @return  
	 */  
	public boolean regist(User user) {

		UserExample example = new UserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserNameEqualTo(user.getUserName());
		List<User> list = userMapper.selectByExample(example);
		if(list.size()>0) {
			return false;
		}
		String encodePwd = MD5.MD5Eencryption(user.getUserPassword(), user.getUserName());
		if(encodePwd==null) {
			return false;
		}
		user.setUserPassword(encodePwd);
		user.setEnable((byte) 3 );//注册申请状态码 3
		return (userMapper.insertUser(user)==1);
	}
	
	
	/**
	 * 
	 * <p>Title: showUser</p>  
	 * <p>Description:用户表全查询 </p>  
	 * @author 树昌
	 * @return
	 */
	public List<User> showUser() {
		
		UserExample userExample = new UserExample();
		Criteria criteria = userExample.createCriteria();
		criteria.andEnableNotEqualTo((byte) 0);
		return userMapper.selectByExample(userExample);
	}
	
	/**
	 * 
	 * <p>Title: getByUserId</p >  
	 * <p>Description: 根据userId查找业主 </p >  
	 * @author 泽强
	 * @param userId
	 * @return
	 */
	public User getByUserId(Integer userId) {
		return userMapper.selectByPrimaryKey(userId);
	}
	
	public Map<String, String> modifyPassword(User user, String oldPassword ,String newPassword) {
		Map<String, String> map = new HashMap<>();
		if(oldPassword==newPassword) {
			map.put("msg", "旧密码与新密码相同");
			map.put("staut", "error");
			return map;
		}
		String encodePwd = MD5.MD5Eencryption(oldPassword, user.getUserName());
		UserExample example = new UserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(user.getUserId()).andUserPasswordEqualTo(encodePwd);
		List<User> list = userMapper.selectByExample(example);
		System.out.println(list.size());
		if(list.size()>0) {
			
			encodePwd = MD5.MD5Eencryption(newPassword, user.getUserName());
			if(encodePwd==null) {
				map.put("msg", "新密码无效");
				map.put("staut", "error");
				return map;
			}else {
				user.setUserPassword(encodePwd);
				userMapper.updateByPrimaryKeySelective(user);
				map.put("msg", "修改成功");
				map.put("staut", "success");
				return map;
			}
		}
		map.put("msg", "密码错误");
		map.put("staut", "error");
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: getUserInfo</p>  
	 * <p>Description:根据用户名查询用户所有信息 </p>  
	 * @author 树昌
	 * @param userName
	 * @return
	 */
	public User getUserInfoByUserName(String userName) {
		
		UserExample  userExample = new UserExample();
		Criteria criteria =userExample.createCriteria();
		criteria.andUserNameEqualTo(userName);
		
		List<User> listuser = userMapper.selectByExample(userExample);
		User user = new User();
		if(listuser.size()>0) {
			user = listuser.get(0);
		}
		return user;
	}
	
	
	/**
	 * 
	 * <p>Title: getUserInfoByUserID</p>  
	 * <p>Description:根据用户ID查询用户所有信息 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	public User getUserInfoByUserID(Integer id) {
		
		if(id!=null) {
			User user = userMapper.selectByPrimaryKey(id);
			return user;
		}else {
			return null;
		}
		
	}
}
